<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>资金手动分解 管理</title>
    <jsp:include page="../../import.jsp" flush="true"/>
</head>
<body class="iframeBody">
<form id="capFund_form">
    <div class="fieldset1 tabel_col tabel_col_1 clearFix">
        <div class="caption">
            <span class="title">来款信息</span>
        </div>
        <div class="fieldBox col_33">
            <div class="label">资金编号</div>
            <div class="field">
                ${capFundVO.code}
            </div>
        </div>
        <div class="fieldBox col_33">
            <div class="label">凭证种类</div>
            <div class="field">
                ${capFundVO.certificateCd}
            </div>
        </div>
        <div class="fieldBox col_33">
            <div class="label">凭证号码</div>
            <div class="field">
                ${capFundVO.certificateCode}
            </div>
        </div>

        <div class="fieldBox col_33">
            <div class="label">资金状态</div>
            <div class="field">
                ${capFundVO.fundStatusCdNm}
            </div>
        </div>

        <div class="fieldBox col_33">
            <div class="label">收款户名</div>
            <div class="field">
                ${capFundVO.acceptName}
            </div>
        </div>
        <div class="fieldBox col_33">
            <div class="label">收款账号</div>
            <div class="field">
                ${capFundVO.acceptCode}
            </div>
        </div>
        <div class="fieldBox col_33">
            <div class="label">来款户名</div>
            <div class="field">
                ${capFundVO.comeName}
            </div>
        </div>
        <div class="fieldBox col_33">
            <div class="label">来款账户</div>
            <div class="field">
                ${capFundVO.comeCode}
            </div>
        </div>
        <div class="fieldBox col_33">
            <div class="label">收到金额</div>
            <div class="field">
                <span id="comeAmt"></span>
            </div>
        </div>
        <div class="fieldBox col_33">
            <div class="label">客户名称</div>
            <div class="field">
                ${capFundVO.cstNm}
            </div>
        </div>
        <div class="fieldBox col_33">
            <div class="label">资金到账日期</div>
            <div class="field">
                ${capFundVO.acceptDates}
            </div>
        </div>
        <div class="fieldBox col_33">
            <div class="label">实际到账日期</div>
            <div class="field">
                ${capFundVO.acceptActualDates}
            </div>
        </div>
        <div class="fieldBox col_33">
            <div class="label">待分解金额</div>
            <div class="field">
                <span id="availableAmt" style="color: red;"></span>
            </div>
        </div>
        <div class="fieldBox col_33">
            <div class="label">导入人</div>
            <div class="field">
                ${capFundVO.crtUsrNm}
            </div>
        </div>
        <div class="fieldBox col_33">
            <div class="label">认款人</div>
            <div class="field">
                ${capFundVO.identifyOperatorNm}
            </div>
        </div>
        <div class="fieldBox col_100">
            <div class="label">摘要</div>
            <div class="field">
                ${capFundVO.docketRem}
            </div>
        </div>
    </div>

    <div class="fieldset1 tabel_col clearFix">
        <div class="caption">
            <span class="title">应收列表
                <a class="sq-button-hasicon" href="javascript:;" onclick="qyFundDecom(${capFundVO.id});">查看所有分解项目<i class="sqBtnIco_1"></i></a>
            </span>
        </div>
        <div class="fieldBox col_100">
            <div id="ligergrid_paylist" class="sq-grid"></div>
        </div>

        <div class="fieldBox col_100">
            <div class="label">待分解金额</div>
            <div class="field">
                <span id="enableAmount"></span>
            </div>
        </div>
        <div class="fieldBox col_100">
            <div class="label">虚拟资金流(逾期)</div>
            <div class="field">
                <select name="entryTypCd" id="entryTypCd" >
                    <option value="0">否</option>
                    <option value="1">是</option>
                </select>
            </div>
        </div>

        <div class="fieldBox col_100">
            <div class="label">备注</div>
            <div class="field" style="width:100%">
                <textarea name="docketRem" id="docketRem" class="ui-textarea l-textarea" style="width:99%" value=""
                          rows="4" cols="120" maxlength="200"></textarea>
            </div>
            <input id="fundId" name="fundId" type="text" value="${capFundVO.id}" style="display:none">
            <%--<input id="amount" name="amount" type="text" value="${capFundVO.amount}"  style="display:none">
            <input id="availableAmount" name="availableAmount" type="text" value="${capFundVO.availableAmount}" style="display:none">--%>
        </div>

        <div class="btnsBox" style="text-align: center;">
            <span id="save_Btn" class="sq-button sq-button-round l-dialog-btn-ok">确认分解</span>
            <input type="reset" id="resetBtn" class="l-button l-button-reset Hide">
        </div>
    </div>
</form>
<script>
    var payListGrid=null;
    var available=0;
    var _payno=[];//支付表数组
    $(function () {
        $("#comeAmt").text(FormatMoney(parseFloat('${capFundVO.amount}')/100));
        $("#availableAmt").text(FormatMoney(parseFloat('${capFundVO.availableAmount}')/100));

        $("#capFund_form").ligerForm();
        available=${capFundVO.availableAmount};
        $("#enableAmount").text(FormatMoney(available/100));

        payListGrid = $("#ligergrid_paylist").ligerGrid({
            url: '<%=path%>/capital/capFund/searchBillItemList.do',
            parms:{cstId:'${capFundVO.cstId}',acceptActualDate:'${capFundVO.acceptActualDates}',type:'1'},
            method: 'get',
            columns: [
                {display: '支付表', name: 'paySchNo', width: 200,align: 'left',},
                {display: '应收项目', name: 'feeTypCdNm', width: 150,align: 'center',},
                {display: '应收日期', name: 'payDt', align: 'center', width: 130,render:renderDate},
                {display: '应收金额', name: 'feeAmt', minWidth: 100,align: 'right',
                    render:function(rowdata,index,value){
                        return '&yen;'+FormatMoney(rowdata.feeAmt/100);
                    }
                },
                {display: '已收金额', name: 'paidUpAmt', minWidth: 100,align: 'right',
                    render:function(rowdata,index,value){
                        return '&yen;'+FormatMoney(rowdata.paidUpAmt/100);
                    }
                },
                {display: '剩余金额', name: 'arAmt', minWidth: 100,align: 'right',
                    render:function(rowdata,index,value){
                        return '&yen;'+FormatMoney(rowdata.arAmt/100);
                    }
                },
                {display: '本次实收', minWidth: 100,name:'actAmt',align: 'right',
                    editor: {type: 'number',precision: 2,toFix: true,
                        onToFix: function () {
                            $.ligerDialog.SQalert({
                                SQ_type: "warning",
                                content: "小数点后面最多2位！"
                            });
                        }
                    },
                    render:function(rowdata,index,value){
                        if(value!=null){
                            return '&yen;'+FormatMoney(rowdata.actAmt/100);
                        }
                    }
                },
            ],
            onSelectRow: function (rowdata, rowindex) {
                if(available>=rowdata.arAmt){
                    available=available-rowdata.arAmt;
                    this.updateCell("actAmt",rowdata.arAmt,rowindex);
                }else{
                    this.updateCell("actAmt",available,rowindex);
                    available=0;
                }
                $("#enableAmount").text(FormatMoney(available/100));
            },
            onUnSelectRow:function (rowdata, rowindex){
                available=available+parseInt(rowdata.actAmt);
                this.updateCell("actAmt",0,rowindex);
                $("#enableAmount").text(FormatMoney(available/100));
            },
            onBeforeEdit: function (e) {
                if(!payListGrid.isSelected(e.record)){
                    payListGrid.select(e.record);
                    e.value = e.record.actAmt;
                }
                available=available+parseInt(e.value);
                if(e.value){
                    e.value = parseInt(e.value) / 100;
                }
            },
            onBeforeSubmitEdit: function (e) {
                if(e.value==null || e.value=='' || e.value<0){
                    e.value = 0;
                }
                
                e.value = accMul(parseFloat(e.value),100);
                if(e.value > e.record.arAmt){
                    e.value = e.record.arAmt;
                }
                if(e.value >= parseFloat(available)){
                    e.value = available;
                    available=0;
                }else{
                    available=available-parseInt(e.value);
                }
                var erow=this.getRow(e.rowindex);
                if(available>0){
                    //toggleDisabledRow方法，参数（rowParm, isDisabled）
                    if(erow.__nextid){
                        this.toggleDisabledRow(e.rowindex+1, false);
                    }
                }else{
                    if(erow.__nextid){
                        this.toggleDisabledRow(e.rowindex+1, true);
                    }
                }
                $("#enableAmount").text(FormatMoney(available/100));
            },
            onBeforeSelectRow:function (e) {
                if (e.canRepay != 1) {
                    top.$.ligerDialog.SQalert({content: e.repayPrompt ? e.repayPrompt : "当前无法核销", SQ_type: "warning"});
                    return false;
                }
                if(available==0){
                    top.$.ligerDialog.SQalert({content: "没有可分解金额",SQ_type: "warning"});
                    return false;
                }
            },
            onCheckRow: function (checked, data, rowid, rowOBJ) {
                if(!checked){
                    var nextRow=null;
                    nextRow=this.getRow(data.__nextid);
                    if(nextRow&&nextRow.paySchId!=data.paySchId){
                       return;
                    }
                    do{
                        if(nextRow){
                            if(this.isSelected(nextRow.__id)){
                                this.unselect(nextRow.__id);
                                available+=nextRow.actAmt;
                            }
                            this.toggleDisabledRow(nextRow.__id, !checked);
                            nextRow=this.getRow(nextRow.__nextid);
                        }
                    }while(nextRow&&nextRow.paySchId==data.paySchId);
                }

                if(available>0 && data.canRepay == 1){
                    //toggleDisabledRow方法，参数（rowParm, isDisabled）
                    this.toggleDisabledRow(data.__nextid, !checked);
                }
            },
            isDisabledSelect: function (item) {
                if (item.__previd==-1||_payno.indexOf(item.paySchId)==-1){
                    _payno.push(item.paySchId);
                    return false;
                }
                return true;
            },
            usePager: false,
            root: 'rows',
            record: 'total',
            hideLoadButton: true,
            width: '100%',
            height: 'auto',
            heightDiff: -2,
            enabledEdit: true,
            isScroll: false,
            checkbox: true,
            rownumbers: true,
            allowUnSelectRow: true,
            alternatingRow: false,
            frozen: false,
            groupColumnName:'paySchNo',
            groupColumnDisplay:'支付表号',
            hideCheckAll: true
        });

        /*保存*/
        $("#save_Btn").click(function (event) {
            $(document).trigger('click');
            var form=liger.get("capFund_form");
            var data = form.getData();
            $.ligerDialog.hide();

            var selectedRows = payListGrid.getSelectedRows();
            if (selectedRows.length == 0) {
                $.ligerDialog.SQalert({content: "请选择支付表明细", SQ_type: "warning"});
                return;
            }

            var _pays = [];
            for (var index in selectedRows) {
                if(selectedRows[index].actAmt==null){
                    $.ligerDialog.SQalert({content: "请填写实收金额", SQ_type: "warning"});
                    _pays = [];
                    return;
                }
                if(selectedRows[index].actAmt!=null){
                    var actAmt = 0;
                    if(selectedRows[index].capFlow ==2){
                        actAmt = Math.abs(selectedRows[index].actAmt);
                    }else{
                        actAmt = selectedRows[index].actAmt;
                    }
                    _pays.push({"id":selectedRows[index].id,"feeTypCd":selectedRows[index].feeTypCd,
                        "paySchNo":selectedRows[index].paySchNo,"capFlow":selectedRows[index].capFlow,
                        "paySchId":selectedRows[index].paySchId,"actAmt":actAmt,
                        "bizTypCd":selectedRows[index].bizTypCd
                    });
                }
            }
            data["payList"]=JSON.stringify(_pays);

            var wait_dialog;
            $.ajax({
                type: 'post',
                dataType: 'json',
                data: data,
                url: '<%=path%>/capital/capFund/decompose.do',
                beforeSend: function () {
                    wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
                },
                success: function (msg) {
                    wait_dialog.close();
                    if (msg.success == 1) {
                        top.$.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                        //回调刷新列表
                        var callback=frameElement.W2W.callback;
                        callback();
                        Remove_navTab();
                    } else {
                        top.$.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                    }
                }
            });
        });


    });

    function accMul(arg1,arg2){
        var m=0,s1=arg1.toString(),s2=arg2.toString();
        try{m+=s1.split(".")[1].length}catch(e){}
        try{m+=s2.split(".")[1].length}catch(e){}
        return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
    }
    
    //查询资金分解详情
    function qyFundDecom(fundId){
        Add_navTab('toDecomDtlPage_'+fundId, "分解单查看", '<%=path%>/capital/capFund/toDecomDtlPage.do?fundId='+fundId, null);
    }

    //格式化时间
    function renderDate(rowdata, index, value) {
        var dateStr = "";
        if (null != value) {
            var date = new Date(value);
            dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
        }
        return dateStr;
    }

</script>
</body>
</html>
